package nc.bs.hrppl.util;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;

import java.util.HashMap;
import java.util.Map;

/**
 * 获取预算编制数据
 */
public class QueryBudgetMakeDetailUtil {
    private BaseDAO baseDao;

    /**
     * 获取预算编制中的 本年最终预算数和调整数
     * @param pk_project 项目id
     * @return
     * @throws BusinessException
     */
    public static Map<String, UFDouble>  getBudgetMakeDetailData(Object pk_project) throws BusinessException {
        QueryBudgetMakeDetailUtil util = new QueryBudgetMakeDetailUtil();
        Map<String, UFDouble> data = new HashMap<>();
        String sql = util.buildQueryTZAndZZData(pk_project);
        Object o = util.getBaseDao().executeQuery(sql, new MapProcessor());
        if (o != null ) {
            data = (Map<String, UFDouble>) o;
            UFDouble tzmonery = data.get("tzmonery") == null ? new UFDouble(0) : new UFDouble(data.get("tzmonery")+"");
            UFDouble zzmonery = data.get("zzmonery") == null ? new UFDouble(0) : new UFDouble(data.get("zzmonery")+"");
            data.put("tzmonery", tzmonery);
            data.put("zzmonery", zzmonery);
        }else{
            data.put("tzmonery", new UFDouble(0));
            data.put("zzmonery", new UFDouble(0));


        }
        return data;
    }

    /**
     * 获取预算编制中本年最终预算数与调整数据
     * @param pk_project 项目id
     * @return
     * @throws BusinessException
     */
    protected  String buildQueryTZAndZZData(Object pk_project) throws BusinessException {
        String sql = " select " +
                "         amount_adjust tzmonery, amount zzmonery " +
                "  from ( " +
                "         select  * " +
                "         from hrpbm_budgetmakedetail " +
                "         where pk_project = '" + pk_project + "' " +
                "         order by ts desc  " +
                "   ) " +
                "  where rownum = 1";
        return sql;
    }
    public BaseDAO getBaseDao() {
        if (null == this.baseDao) {
            this.baseDao = new BaseDAO();
        }
        return this.baseDao;
    }
}
